<template>
  <el-dialog
      :model-value="dialogVisible"
      :title="props.dialogJcTitle"
      width="40%"
      @close="handleClose"
  >
    <el-form :model="form" ref="formRef" label-width="90px" :rules="rules">
      <el-form-item label="样本名称" prop="cpmc">
        <el-select v-model="form.cpmc" clearable placeholder="请选择">
          <el-option
              v-for="(item, index) in CpName"
              :key="index"
              :label="item"
              :value="item"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="批次号" prop="pch">
        <el-select v-model="form.pch" clearable placeholder="请选择">
          <el-option
              v-for="(item, index) in pchOptions"
              :key="index"
              :label="item"
              :value="item"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="检测内容" prop="jcnr">
        <el-input v-model="form.jcnr" />
      </el-form-item>
      <el-form-item label="检测值" prop="jcz">
        <el-input  v-model="form.jcz" />
      </el-form-item>
      <el-form-item label="检测结果">
        <el-input v-model="form.jcjg" />
      </el-form-item>
      <el-form-item label="检测地点">
        <el-input v-model="form.jcdd" />
      </el-form-item>
      <el-form-item label="检测人" prop="jcr">
        <el-input v-model="form.jcr" />
      </el-form-item>
      <el-form-item label="检测日期" prop="rq">
        <el-date-picker
            v-model="form.rq"
            type="date"
            placeholder="选择日期"
            :size="'default'"
            clearable
        />
      </el-form-item>
      <el-form-item label="图片名称">
        <el-input disabled v-model="tpmc" />
      </el-form-item>
      <el-form-item label="检测报告">
        <my-upload
            :file-type="'jcsdInfo'"
            @get-form-pic-value="getFormPicValue"
        >
        </my-upload>
      </el-form-item>
    </el-form>
    <template #footer>
      <span>
        <el-button @click="handleClose">取消</el-button>
        <el-button type="primary" @click="handleConfirm">
          确认
        </el-button>
      </span>
    </template>
  </el-dialog>
</template>

<script setup>
import {defineEmits, ref, defineProps, watch, onMounted} from 'vue'
import {getAllCpmc, getAllPcCpmc, getCpmc, getPchByCpmc, getPchInfo} from "@/api/apiPath/twoPart.js";
import {getOptionInfo, getOptionsInfo} from "@/api/methods/initInfo.js";
import MyUpload from "@/components/commons/myUpload.vue";

const props = defineProps({
  dialogJcTitle: {
    type: String,
    default: '',
    required: true
  },
  dialogJcTableValue: {
    type: Object,
    default: {}
  },
  queryForm: {
  },
  dialogVisible: {
    type: Boolean,
    default: false
  }
})
const CpName = ref([])
const pchOptions = ref([])
const formRef = ref(null)
const tpmc = ref('')
const form = ref({
  cpmc: '',
  pch: '',
  jcnr: '',
  jcz: '',
  jcjg: '',
  jcdd: '',
  jcr: '',
  rq: '',
  pic: '',
})
const getFormPicValue = (value)=> {
  console.log(value)
  form.value.pic = value.pic
  tpmc.value = value.tpmc
}
onMounted(async ()=> {
    CpName.value = await getOptionInfo(getAllPcCpmc())
    form.value = props.dialogJcTableValue
})
watch(()=> form.value.cpmc, async (cpmc)=> {
  console.log(cpmc)
  if (form.value.cpmc !== null && form.value.cpmc !== "") {
    pchOptions.value = await getOptionInfo(getPchInfo(cpmc))
    console.log(pchOptions.value)
    form.value.pch = ''
  }
}, {deep: true, immediate: true})
/*
1. 第一个参数：监听的数据源
(1)-个ref
(2)一个响应式对象
(3)一个函数，返回一个值
(4)以上类型值组成的数组
2. 第二个参数：发生变化时要调用的函数
这个函数有三个参数：新值，旧值，清除函数
清除函数：下一次执行副作用前调用
3. 第三个参数：一个对象，配置项
deep:如果源是对象，则配置为true可以监听到
immediate:在监听器创建的时候立即触发
*/

const rules = ref({
  pch: [{
    required: true,
    message: '不能为空',
    trigger: ['blur', 'change'],
  }],
  cpmc: [{
    required: true,
    message: '不能为空',
    trigger: ['blur', 'change'],
  }],
  jcr: [{
    required: true,
    message: '不能为空',
    trigger: ['blur', 'change'],
  }],
})
const emits = defineEmits(['update:modelValue', 'handleConfirm'])
const handleClose = ()=> {
  emits('update:modelValue', false)
}
const handleConfirm = () => {
  emits("handleConfirm", {...form.value}, {...formRef.value})
}
</script>

<style scoped>


</style>